package com.supersit.greensproject.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.supersit.greensproject.AsyncTask.UploadDataSelectResult;
import com.supersit.greensproject.R;
import com.supersit.greensproject.adapter.SubjectDataAdapter;
import com.supersit.greensproject.application.MyApplication;
import com.supersit.greensproject.entity.DataUpload;
import com.supersit.greensproject.fragment.UploadDataEnteringFragment;
import com.supersit.greensproject.fragment.UploadDataSelectFragment;
import com.supersit.greensproject.resources.Authority;
import com.supersit.greensproject.resources.Resources;
import com.supersit.greensproject.utils.NetUtils;
import com.supersit.greensproject.utils.SPUtils;
import com.supersit.greensproject.view.MyTitleBar;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cong on 2016/7/16.
 */
@ContentView(R.layout.activity_upload_data)
public class UploadDataActivity extends BaseActivity {
    @ViewInject(R.id.lv_upload_data_list)
    private ListView lvUpLoadDataList;
    private ArrayList<Map<Integer, String>> itemData;
    private SubjectDataAdapter adapter;
    private Context context;
    private MyTitleBar titleBar;
    private int[] baseAuthority;
    private List<DataUpload> dataUploadLists;
    private UploadDataSelectResult uploadDataSelectResult;
    private int userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        context = this;
        setTitleBar();
        bindListener();
        setListViewData();
        initVariable();
    }

    private void initVariable() {
        if (NetUtils.isConnected(context)) {
            userId = (int) SPUtils.get(context, "userId", -1);
            uploadDataSelectResult = new UploadDataSelectResult(context, userId, null);
            uploadDataSelectResult.getUploadNetData();//更新表数据
        } else {
           // T.showShort(context, R.string.net_not_connected);
        }
    }

    private void setTitleBar() {
        titleBar = new MyTitleBar(context, findViewById(R.id.upload_data_title));
        titleBar.setTitleText(getResources().getString(R.string.upload_data));
        titleBar.setChildVisibility(false, titleBar.getmImgRight());

    }

    private void bindListener() {
        titleBar.setImg1Listener(onClickListener);
        lvUpLoadDataList.setOnItemClickListener(onItemClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.title_bar_left:
                    finish();
                    break;
            }
        }
    };

    private void setListViewData() {
        baseAuthority = Authority.getBaseAuthority(context);
        itemData = new ArrayList<Map<Integer, String>>();
        for (int i = 0; i < baseAuthority.length; i++) {
            for (int j = 0; j < Resources.uploadDataMenu.length; j++) {
                if (baseAuthority[i] == j) {
                    Map<Integer, String> map = new HashMap<Integer, String>();
                    map.put(baseAuthority[i], Resources.uploadDataMenu[baseAuthority[i]]);
                    itemData.add(map);
                }
            }
        }
        adapter = new SubjectDataAdapter(context
                , itemData, Resources.NoMessige);
        lvUpLoadDataList.setAdapter(adapter);
    }

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int num = 0;
            Map<Integer, String> item = itemData.get(position);
            for (int key : item.keySet()) {
                num = key;
            }
            UploadDataEnteringFragment enteringFragment = new UploadDataEnteringFragment();
            UploadDataSelectFragment selectFragment = new UploadDataSelectFragment();
            Intent intent = new Intent(context, UploadDataFragmentActivity.class);
            intent.putExtra("merchandise", num);
            switch (num) {
                case 0:
                    MyApplication.list.clear();
                    MyApplication.list.add(selectFragment);
                    MyApplication.list.add(enteringFragment);
                    startActivity(intent);
                    break;
                case 1:
                    MyApplication.list.clear();
                    MyApplication.list.add(selectFragment);
                    MyApplication.list.add(enteringFragment);
                    startActivity(intent);
                    break;
                case 2:
                    MyApplication.list.clear();
                    MyApplication.list.add(selectFragment);
                    MyApplication.list.add(enteringFragment);
                    startActivity(intent);
                    break;
                case 3:
                    MyApplication.list.clear();
                    MyApplication.list.add(selectFragment);
                    MyApplication.list.add(enteringFragment);
                    startActivity(intent);
                    break;
                case 4:
                    MyApplication.list.clear();
                    MyApplication.list.add(selectFragment);
                    MyApplication.list.add(enteringFragment);
                    startActivity(intent);
                    break;
                default:
                    break;
            }
        }
    };
}
